#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"

#include "request.pb.h"

#include <iostream>
#include <unordered_map>

#define _1 std::placeholders::_1
#define _2 std::placeholders::_2
#define _3 std::placeholders::_3

class Server
{
public:
    typedef std::shared_ptr<myrequest::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<myrequest::AddResquest> AddResquestPtr;
    typedef std::shared_ptr<myrequest::TranslateRespone> TranslateResponsePtr;
    typedef std::shared_ptr<myrequest::AddRespone> AddResponsePtr;
public:
    Server(const uint16_t port)
    : _server(&_baseloop, muduo::net::InetAddress("0.0.0.0", port), "Server", muduo::net::TcpServer::kReusePort)
    , _dispatcher(std::bind(&Server::onUnknownMessage, this, _1, _2, _3))
    , _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, _1, _2, _3))
    {
        _dispatcher.registerMessageCallback<myrequest::TranslateRequest>(
            std::bind(&Server::onTranslate, this, _1, _2, _3));
        _dispatcher.registerMessageCallback<myrequest::AddResquest>(
            std::bind(&Server::onAdd, this, _1, _2, _3));
        _server.setConnectionCallback(
            std::bind(&Server::onConnection, this, _1));
        _server.setMessageCallback(
            std::bind(&ProtobufCodec::onMessage, &_codec, _1, _2, _3));
    }
    void Start()
    {
        _server.start();
        _baseloop.loop();
    }
private:
    void onAdd(const muduo::net::TcpConnectionPtr& conn,
               const AddResquestPtr& message,
               muduo::Timestamp)
    {
        uint32_t num1 = message->op1();
        uint32_t num2 = message->op2();
        myrequest::AddRespone resp;
        resp.set_result(num1 + num2);
        _codec.send(conn, resp);
    }
    std::string Translate(const std::string& str)
    {
        static std::unordered_map<std::string, std::string> dict = {
            {"hello", "你好"},
            {"你好", "hello"}
        };
        auto it = dict.find(str);
        if (it == dict.end()) return "None!";
        return it->second;
    }
    void onTranslate(const muduo::net::TcpConnectionPtr& conn,
               const TranslateRequestPtr& message,
               muduo::Timestamp)
    {
        // 1.提取有效信息
        // 2.业务处理
        // 3.组织响应
        // 4.发送
        std::string req_msg = message->msg();
        std::string resp_msg = Translate(req_msg);
        myrequest::TranslateRespone resp;
        resp.set_msg(resp_msg);
        _codec.send(conn, resp);
    }
    void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if (conn->connected())
        {
            LOG_INFO << "新连接成功\n";
        }
        else
        {
            LOG_INFO << "连接断开\n";
        }
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn,
                        const MessagePtr& message,
                        muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }
private:
    muduo::net::EventLoop _baseloop;
    muduo::net::TcpServer _server; // tcp服务器对象
    ProtobufDispatcher _dispatcher; // 分发器
    ProtobufCodec _codec; //协议器
};

int main()
{
    Server server(8888);
    server.Start();

    return 0;
}